package cn.yun.web.response;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/*
演示ServletResponse对象:
     重定向功能:response.sendRedirect("路径");
    动态获取虚拟目录: request.getContextPath
*/
@WebServlet("/responseDemo1")
public class ResponseDemo1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        System.out.println("demo1...");
/*
          //访问responseDemo1会自动跳转到responseDemo2资源
        //1.设置响应状态码为302-->表示重定向
        response.setStatus(302);
        //2.设置响应头location
        response.setHeader("location","/day15/responseDemo2");*/

        //简单的重定向方法
       // response.sendRedirect("/day15/responseDemo2");
       // response.sendRedirect("http://www.baidu.com");

        //动态获取虚拟目录
        String contextPath = request.getContextPath();
        response.sendRedirect(contextPath+"/responseDemo2");

        request.setAttribute("msg","hello");

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
